package ConnectData;

import java.sql.*;

public class getConnections {

    public getConnections() {
    }

    public static Connection getConnection1(String userName, String password) {
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection cn = DriverManager.getConnection("jdbc:odbc:" + Config.SqlServer, userName, password);
            return cn;
        } catch (ClassNotFoundException ex) {
            System.err.println("Loi 1:");
            ex.printStackTrace();
        } catch (SQLException ex) {
            System.err.println("Loi 2:");
            ex.printStackTrace();
        }
        return null;
    }
  

    public static Connection getConnection4(String serverName,String DataName, String userName, String password) {
       
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String sURL ="jdbc:sqlserver://" + serverName
                         + ":"+Config.PortNum+";databaseName=" + DataName
                            + ";User=" + userName
                                 + ";PassWord=" + password;
            Connection cn = DriverManager.getConnection(sURL);
            return cn;
        } catch (ClassNotFoundException ne) {
            System.err.println("Loi 1: Ket noi kieu 4");
            ne.printStackTrace();
        } catch (SQLException se) {
            System.err.println("Loi 2:Ket noi kieu 4");
            se.printStackTrace();
        } catch (Exception e) {
            System.err.println("Loi 3:Ket noi kieu 4");
            e.printStackTrace();
        }
        return null;
        
    }

    public static void closeObject(Object obj) {
        if (obj == null) {
            return;
        }
        try {
            if (obj instanceof Connection) {
                ((Connection) obj).close();
            }
            if (obj instanceof Statement) {
                ((Statement) obj).close();
            }
            if (obj instanceof PreparedStatement) {
                ((PreparedStatement) obj).close();
            }
            if (obj instanceof ResultSet) {
                ((ResultSet) obj).close();
            }
        } catch (SQLException se) {
            se.printStackTrace();
        }
        catch(NullPointerException e)
        {
            e.printStackTrace();
        }
    }
}
